import { Card, message, Table } from 'antd';
import React, { Component } from 'react';
import { connect } from 'dva';
import SearchForm from '../../components/AutoForm/SearchForm';
import EditForm from '../../components/AutoForm/EditForm';

@connect(({ loading, auditOrder }) => ({
  tableLoading: loading.effects['auditOrder/page'],
  formLoading: loading.effects['auditOrder/income'],
  tableData: auditOrder.pageList,
  pagination: auditOrder.pagination,
  columns: auditOrder.columns,
  searchFields: auditOrder.searchFields,
  searchValues: auditOrder.searchValues,
  selectionIds: auditOrder.selectionIds,
  selectedRows: auditOrder.selectedRows,
  moneyModal: auditOrder.moneyModal,
  modalForm: auditOrder.modalForm,
  moneyRows: auditOrder.moneyRows,
}))
export default class NestTable extends Component {
  state = {
    sumText: '选中合计费用：0',
  };

  componentDidMount() {
    this.handleSearch();
  }

  rowClassName = (record, index) => {
    if (record.status === 'SPLIT') {
      return 'orderParent';
    }
  };

  handleSearch = (flag) => {
    const { dispatch, pagination, searchValues } = this.props;
    let pageParam = { pageSize: pagination.pageSize, current: flag==='btn' ? 1 : pagination.current };
    let searchParam = {};
    Object.keys(searchValues).map(key => {
      searchParam[key] = searchValues[key]['value'];
    });
    const param = {
      ...searchParam,
      ...pageParam,
    };
    dispatch({
      type: 'auditOrder/page',
      payload: param,
    });
  };

  handleSearchChange = fields => {
    const { dispatch } = this.props;
    dispatch({
      type: 'auditOrder/handleSearchChange',
      payload: fields,
    });
  };

  handleModalChange = fields => {
    const { dispatch, optFlag, addForm } = this.props;
    const payload = { fields: fields, optFlag: optFlag, addForm: addForm };
    dispatch({
      type: 'auditOrder/handleModalChange',
      payload: payload,
    });
  };

  handleModalVisible = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'auditOrder/handleMoneyModal',
    });
  };

  handleIncomeSub = fields => {
    const { dispatch, selectedRows } = this.props;
    if (selectedRows.length !== 1) {
      return message.error('请选一条要提交的数据！');
    }
    if (selectedRows[0].parentId) {
      return message.error('请选主单提交！');
    }
    const params = {
      ...selectedRows[0],
      ...fields,
    };
    dispatch({
      type: 'auditOrder/income',
      payload: params,
    }).then(() => {
      this.handleSearch();
    });
  };

  onChange = (selectedRowKeys, selectedRows) => {
    let sum = 0.0;
    selectedRows.forEach(row => {
      sum += row.feeSum;
    });
    this.setState({ sumText: `选中合计费用：${sum}` });
    const { dispatch } = this.props;
    dispatch({
      type: 'auditOrder/handleSelection',
      payload: { selectedRowKeys, selectedRows },
    });
  };

  onSend = () => {
    const { dispatch, selectedRows } = this.props;
    if (selectedRows.length !== 1) {
      return message.error('请选一条要提交的数据！');
    }
    if (selectedRows[0].parentId) {
      return message.error('请选主单提交！');
    }
    selectedRows[0].status = 'GATHERING';
    dispatch({
      type: 'auditOrder/audit',
      payload: selectedRows[0],
    }).then(() => {
      this.handleSearch();
    });
  };

  onMoney = () => {
    const { dispatch, selectedRows } = this.props;
    if (selectedRows.length !== 1) {
      return message.error('请选一条要提交的数据！');
    }
    if (selectedRows[0].parentId) {
      return message.error('请选主单填写！');
    }
    dispatch({
      type: 'auditOrder/handleMoneyModal',
    });
  };

  render() {
    const {
      columns,
      pagination,
      tableLoading,
      tableData,
      searchFields,
      searchValues,
      moneyModal,
      modalForm,
      moneyRows,
      formLoading,
      dispatch,
    } = this.props;
    pagination.onChange = (page, pageSize) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'auditOrder/page',
        payload: {
          ...searchParam,
          current: page,
          pageSize,
        },
      });
    };
    pagination.onShowSizeChange = (current, size) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'auditOrder/page',
        payload: {
          ...searchParam,
          current: current,
          pageSize: size,
        },
      });
    };

    const rowSelection = {
      type: 'checkbox',
      selections: this.props.selectedRows,
      selectedRowKeys: this.props.selectionIds,
      onChange: this.onChange,
      getCheckboxProps: record => ({
        disabled: record.parentId,
      }),
    };

    return (
      <Card bodyStyle={{ padding: 0 }}>
        <SearchForm
          header="审单->操作"
          moneyText="填写实际收入"
          layout="inline"
          onFieldsChange={this.handleSearchChange}
          onSubmit={() => {this.handleSearch('btn')}}
          onSend={this.onSend}
          isSend={true}
          isSum={true}
          sumText={this.state.sumText}
          isMoney={true}
          onMoney={this.onMoney}
          fields={searchFields}
          fieldValue={searchValues}
        />
        <Table
          loading={tableLoading}
          columns={columns}
          rowSelection={rowSelection}
          dataSource={tableData}
          size="small"
          bordered
          rowKey="id"
          scroll={{ x: 'max-content' }}
          rowClassName={this.rowClassName}
          pagination={pagination}
        />

        <EditForm
          rows={moneyRows}
          title="填写实际收入"
          fieldValue={modalForm}
          onFieldsChange={this.handleModalChange}
          onCancel={this.handleModalVisible}
          onOk={this.handleIncomeSub}
          confirmLoading={formLoading}
          visible={moneyModal}
        />
      </Card>
    );
  }
}
